<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
include_once("../../config.inc.php");
session_start();
$_LangPath = "changewagerList";
include_once(__Admin_Path."/logincheck.php");
include_once(__Language_Path."/selectlanguage_admin.php");
include_once(__Modules_Path."/Gamble.lib.php");
include_once(__Modules_Path."/LogRecord.lib.php");
include_once(__Common_Path."/SysParam.inc.php");

$arrPower = CheckUsePower("changewagerlist");
if($arrPower === false)ErrMsgOutputHTML("Error", "", 1);

/*$strSQL = "select Password from `bet_Member` where ID = ".$_SESSION["Admin_Info"] -> ID;
if(!$RS = @PMA_mysql_query($strSQL, $_ConnMaster)){
	$GLOBALS["errormessage"] = "Find User Data Error!!";
	$GLOBALS["errorhidden"] = "\n Error: ".PMA_mysql_error($_ConnMaster).",\nSQL: ".$strSQL.",\nLine: ".__LINE__.",\nPage: ".__FILE__;
	ErrMsgOutputHTML("Error", "", 1);
}

$isPWD = false;
$_data["ChangePWD"] = strtolower($_data["ChangePWD"]);
if(PMA_mysql_num_rows($RS) > 0){
	$Rows = PMA_mysql_fetch_object($RS);
	$strChangePWD = strtolower($Rows -> Password);
	if($_data["ChangePWD"] == $strChangePWD)$isPWD = true;
}

if(!$isPWD){
	$_SESSION["ChangePWD_ErroNO"]++;
	if($_SESSION["ChangePWD_ErroNO"] > 5){
		@PMA_mysql_query("update `bet_Member` set isLock = 1 where ID = ".$_SESSION["Admin_Info"] -> ID, $_ConnMaster);
		if(!getOutUser($_SESSION["AdminUser_ID"])){
			ErrMsgOutputHTML("Error", "", 1);
		}
		$Result .= "\n\n停用此帳號！！\n";
		$GLOBALS["errormessage"] = "Sorry!! Wrong password over 5 times, system will stop your account!!";
	}else{
		$GLOBALS["errormessage"] = "Password Error, ".$_SESSION["ChangePWD_ErroNO"]." times!!";
	}
	$Result .= "\n動作=".$_data["isAccept"]."\n";
	$Result .= "注單=".$_data["BID"]."\n";
	$Result .= "密碼=".$_data["ChangePWD"]."\n";
	$Result .= "次數=".$_SESSION["ChangePWD_ErroNO"]."\n";
	AddData_Log($_SESSION["Admin_Info"] -> ID, "更改注單狀態--密碼錯誤", "管理者(".$_SESSION["Admin_Info"] -> Account."--".$_SESSION["Admin_Info"] -> Name.")更改注單狀態--密碼錯誤：".$Result);
	ErrMsgOutputHTML("Error", "", 1);
}
*/
if($arrPower[1] != "all"){
	if($arrPower[1] != "supple"){
		//$GLOBALS["errormessage"] = "No Power!!";
		//ErrMsgOutputHTML("Error", "", 1);

		$strSQL = "select ID from`bet_Member` where Account = '".$_KSysParam[SuppleReAccount]."'";
		if(!$RS = @PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Find Wager Data Error!!";
			$GLOBALS["errorhidden"] = "\n Error: ".PMA_mysql_error($_ConnMaster).",\nSQL: ".$strSQL.",\nLine: ".__LINE__.",\nPage: ".__FILE__;
			ErrMsgOutputHTML("Error", "", 1);
		}
		if(PMA_mysql_num_rows($RS)){
			$Rows = PMA_mysql_fetch_object($RS);
			$strSQLAdd = " and MemID != ".$Rows -> ID;
		}

		$arrGame = getMoreGambleData("", "", "0", 0, "");
		if($arrGame === false){
			$GLOBALS["errormessage"] = "Find Gamble Data Error!!";
			$GLOBALS["errorhidden"] = "\n Error: ".PMA_mysql_error($_ConnMaster).",\nSQL: ".$strSQL.",\nLine: ".__LINE__.",\nPage: ".__FILE__;
			ErrMsgOutputHTML("Error", "", 1);
		}
		if(sizeof($arrGame) == 0){
			$GLOBALS["errormessage"] = $_strChangeWagerList["GameIsOver"];
			ErrMsgOutputHTML("Error", "", 1);
		}
		$arrGID = array(0);
		foreach($arrGame as $intGameID => $arrG){
			$arrGID[] = $intGameID;
		}
		$strSQLAdd .= " and GambleID in (".implode(",", $arrGID).")";
	}
	$strSQL = "select ID, GambleID from `bet_BetList` where ID in (".$_data["BID"].") and WagerType = '1'".$strSQLAdd;
	//exit($strSQL);
	if(!$RS = @PMA_mysql_query($strSQL, $_ConnMaster)){
		$GLOBALS["errormessage"] = "Find Wager Data Error!!";
		$GLOBALS["errorhidden"] = "\n Error: ".PMA_mysql_error($_ConnMaster).",\nSQL: ".$strSQL.",\nLine: ".__LINE__.",\nPage: ".__FILE__;
		ErrMsgOutputHTML("Error", "", 1);
	}

	$_data["BID"] = "0";
	if(PMA_mysql_num_rows($RS)){
		$arrBID = array();
		$arrGID = array();
		while($Rows = PMA_mysql_fetch_object($RS)){
			$arrBID[] = $Rows -> ID;
			$arrGID[$Rows -> GambleID] = $Rows -> GambleID;
		}
		$_data["BID"] = join(",", $arrBID);
	}
}

$_SESSION["ChangePWD_ErroNO"] = 0;
if($_data["isAccept"] == 0){
	$strSQL = "update `bet_BetList` set isPayOff = 0 where ID in (".$_data["BID"].")";
	$strSQLTmp = "update `bet_BetListTmp` set isPayOff = 0 where ID in (".$_data["BID"].")";
	$strSQLPNE = "update `bet_ParlaysNumberEven` set isPayOff = 0 where BetListID in (".$_data["BID"].")";
	$strSQLQ = " and isPayOff < 0 ";
	$intCType = 0;
	$intMulti = 1;
}elseif($_data["isAccept"] == -1){
	$strSQL = "update `bet_BetList` set isPayOff = -1, MemResult = 0, SubIncome = 0, SubResult = 0, SubRateResult = 0, MainResult = 0, MainRateResult = 0, StockResult = 0, StockRateResult = 0, DirectorResult = 0, DirectorRateResult = 0 where ID in (".$_data["BID"].")";
	$strSQLTmp = "update `bet_BetListTmp` set isPayOff = -1, MemResult = 0, SubIncome = 0, SubResult = 0, SubRateResult = 0, MainResult = 0, StockResult = 0, DirectorResult = 0 where ID in (".$_data["BID"].")";
	$strSQLPNE = "update `bet_ParlaysNumberEven` set isPayOff = -1 where BetListID in (".$_data["BID"].")";
	$strSQLQ = " and isPayOff >= 0 ";
	$intCType = -1;
	$intMulti = -1;
}elseif($_data["isAccept"] == -2 && $arrPower[1] == "all"){
	$strSQL = "update `bet_BetList` set isPayOff = -2, MemResult = 0, SubIncome = 0, SubResult = 0, SubRateResult = 0, MainResult = 0, MainRateResult = 0, StockResult = 0, StockRateResult = 0, DirectorResult = 0, DirectorRateResult = 0 where ID in (".$_data["BID"].")";
	$strSQLTmp = "update `bet_BetListTmp` set isPayOff = -2, MemResult = 0, SubIncome = 0, SubResult = 0, SubRateResult = 0, MainResult = 0, StockResult = 0, DirectorResult = 0 where ID in (".$_data["BID"].")";
	$strSQLPNE = "update `bet_ParlaysNumberEven` set isPayOff = -2 where BetListID in (".$_data["BID"].")";
	$strSQLQ = " and isPayOff >= 0 ";
	$intCType = -2;
	$intMulti = -1;
}

PMA_mysql_query("BEGIN", $_ConnMaster);

$strSQLQ = "select LottoID, Bet_Type, GambleID, Number, WagerType, COUNT(*) as tolWager, SUM(MemStake) as tolStake, sum(MemPayOff) as tolPayOff, sum(MemStake*MemExchangeRate*DirectorRate*DirectorCommission) as tolCommission, sum(MemStake*MemExchangeRate*DirectorRate) as tolRealStake, sum(MemPayOff*MemExchangeRate*DirectorRate) as tolRealPayOff from `bet_BetListTmp` where ID in (".$_data["BID"].") ".$strSQLQ." Group By GambleID, Bet_Type, Number, WagerType";
if(!$RS = PMA_mysql_query($strSQLQ, $_ConnMaster)){
	$GLOBALS["errormessage"] = "System Error!!";
	$GLOBALS["errorhidden"] = "\n Error: ".PMA_mysql_error($_ConnMaster).",\nSQL: ".$strSQL.",\nLine: ".__LINE__.",\nPage: ".__FILE__;
	PMA_mysql_query("ROLLBACK", $_ConnMaster);
	ErrMsgOutputHTML("Error", "", 1);
}

if(PMA_mysql_num_rows($RS) > 0){
	while($Rows = PMA_mysql_fetch_object($RS)){
		if($Rows -> WagerType == 0 || $Rows -> WagerType == 2 || $Rows -> WagerType == 3 || $Rows -> WagerType == 4){
			$strOhterCase = " tolWager = tolWager + ".$intMulti*$Rows -> tolWager.", tolStake = tolStake + ".$intMulti*$Rows -> tolStake.", tolRealStake = tolRealStake + ".$intMulti*$Rows -> tolRealStake.", tolCommission = tolCommission + ".$intMulti*$Rows -> tolCommission.", tolPayOff = tolPayOff + ".$intMulti*$Rows -> tolPayOff.", tolRealPayOff = tolRealPayOff + ".$intMulti*$Rows -> tolRealPayOff;

		}elseif($Rows -> WagerType == 1){
			$strOhterCase = " tolSuppleWager = ".($intMulti*$Rows -> tolWager)." + tolSuppleWager, tolSuppleStake = tolSuppleStake + ".($intMulti*$Rows -> tolRealStake*-1).", tolSupplePayOff = tolSupplePayOff + ".($intMulti*$Rows -> tolRealPayOff*-1).", tolSuppleCommission = tolSuppleCommission + ".($intMulti*$Rows -> tolCommission*-1);
		}else{
			PMA_mysql_query("ROLLBACK", $_ConnMaster);
			continue;
		}
		if(!UpdateTodayGameOhterCase($Rows -> GambleID, $Rows -> LottoID, $Rows -> Bet_Type, $Rows -> Number, $strOhterCase)){
			ErrMsgOutputHTML("Error", "", 1);
		}
	}
}
//print $strSQL."<br>\n";
if(!PMA_mysql_query($strSQL, $_ConnMaster)){
	$GLOBALS["errormessage"] = "System Error!!";
	$GLOBALS["errorhidden"] = "\n Error: ".PMA_mysql_error($_ConnMaster).",\nSQL: ".$strSQL.",\nLine: ".__LINE__.",\nPage: ".__FILE__;
	PMA_mysql_query("ROLLBACK", $_ConnMaster);
	ErrMsgOutputHTML("Error", "", 1);
}
//print $strSQLTmp."<br>\n";
if(!PMA_mysql_query($strSQLTmp, $_ConnMaster)){
	$GLOBALS["errormessage"] = "System Error!!";
	$GLOBALS["errorhidden"] = "\n Error: ".PMA_mysql_error($_ConnMaster).",\nSQL: ".$strSQLTmp.",\nLine: ".__LINE__.",\nPage: ".__FILE__;
	PMA_mysql_query("ROLLBACK", $_ConnMaster);
	ErrMsgOutputHTML("Error", "", 1);
}

if(!PMA_mysql_query($strSQLPNE, $_ConnMaster)){
	$GLOBALS["errormessage"] = "System Error!!";
	$GLOBALS["errorhidden"] = "\n Error: ".PMA_mysql_error($_ConnMaster).",\nSQL: ".$strSQLPNE.",\nLine: ".__LINE__.",\nPage: ".__FILE__;
	PMA_mysql_query("ROLLBACK", $_ConnMaster);
	ErrMsgOutputHTML("Error", "", 1);
}

PMA_mysql_query("COMMIT", $_ConnMaster);

$arrBID = array();
if($_data["BID"] != "")$arrBID = split(",", $_data["BID"]);
if(sizeof($arrBID) > 0){
	foreach($arrBID as $key => $intBID){
		$arrBID[$key] = "('".$_SESSION["Admin_Info"] -> ID."', '".$intBID."', '".$intCType."', '".$_SERVER["REMOTE_ADDR"]."', Now())";
	}
	$strSQL = "insert into `bet_ChangeWagerStatusLog` (UserID, WagerID, CType, IP, AddTime) values".join(",", $arrBID);
	if(!PMA_mysql_query($strSQL, $_ConnMaster)){
		$GLOBALS["errormessage"] = "System Error!!";
		$GLOBALS["errorhidden"] = "\n Error: ".PMA_mysql_error($_ConnMaster).",\nSQL: ".$strSQL.",\nLine: ".__LINE__.",\nPage: ".__FILE__;
		ErrMsgOutputHTML("Error", "", 1);
	}
}
@mysql_close($$_Conn);
header("Location: ChangeWagerList.php?BID=".$_data["arrBID"]);
?>